//
//  MyScrollerView.m
//  TEXT
//
//  Created by dllo on 9/25/15.
//  Copyright (c) 2015 King. All rights reserved.
//

#import "MyScrollerView.h"

@interface MyScrollerView()

@property (nonatomic, strong) UICollectionView *scrollView;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, assign) BOOL isTimeUp;
@property (nonatomic, strong) NSTimer *moveTime;

@end

@implementation MyScrollerView


static CGFloat const chageImageTime = 3.0;

+ (instancetype)shareScrollerViewWithUrl:(NSURL *)url
{
    static MyScrollerView *scroller = nil;
    dispatch_once_t once;
    dispatch_once(&once, ^{
        scroller = [[MyScrollerView alloc]init];
        scroller.imageArr = [NSMutableArray array];
    });
    return scroller;
}

-(void)getImageArr:(NSMutableArray *)imageArr
{
    NSString *firstImg = [imageArr firstObject];
    NSString *lastImg = [imageArr lastObject];
    [imageArr addObject:firstImg];
    [imageArr insertObject:lastImg atIndex:0];
    self.imageArr = imageArr;
    [self.scrollView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.minimumInteritemSpacing = 0;
        flow.minimumLineSpacing = 0;
        flow.itemSize = CGSizeMake(SelfWidth, SelfHeight);
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        self.scrollView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SelfWidth, SelfHeight) collectionViewLayout:flow];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.pagingEnabled = YES;
        _scrollView.contentOffset = CGPointMake(SelfWidth, 0);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.dataSource = self;
        [_scrollView registerClass:[LuboCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];

        self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(SelfWidth / 2 - 10, SelfWidth * 2 / 3 - 30, 20, 20)];
        _page.currentPageIndicatorTintColor = [UIColor colorWithRed:0 green:220.0 / 256.0 blue:220.0 / 256.0 alpha:1.0];
        _page.pageIndicatorTintColor = [UIColor grayColor];
        _page.currentPage = 0;
       
        [self addSubview:_scrollView];
        [self addSubview:_page];
        [self moveTimeUp];
    }
    return self;
}

- (void)moveTimeUp
{
    _moveTime = [NSTimer scheduledTimerWithTimeInterval:chageImageTime target:self selector:@selector(animalMoveImage) userInfo:nil repeats:YES];
}

- (void)stopTimeUp
{
    [_moveTime invalidate];
}

#pragma mark - collectionView 代理方法的实现 给对应位置图片
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    _page.numberOfPages = _imageArr.count - 2;
    return _imageArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LuboCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.pic.image = _imageArr[indexPath.row];
    return cell;
}

#pragma mark - 计时器到时,系统滚动图片
- (void)animalMoveImage
{
    [self.scrollView setContentOffset:CGPointMake(SelfWidth* (_page.currentPage + 2), 0) animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger i = SelfWidth / 2;
    self.page.currentPage = (scrollView.contentOffset.x - SelfWidth - i) / SelfWidth + 1;
    if (self.scrollView.contentOffset.x <= 0)
    {
        self.scrollView.contentOffset = CGPointMake(SelfWidth * (self.imageArr.count - 2), 0);
    }
    else if(self.scrollView.contentOffset.x >= SelfWidth * (self.imageArr.count - 1))
    {
        self.scrollView.contentOffset = CGPointMake(SelfWidth, 0);
    }

}

#pragma mark - 停止拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self moveTimeUp];
}

#pragma mark - 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimeUp];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
